<?php

class ReconocimientoFacial
{
	public $rootPath;
	public $galleryPath;
	public $pyfacesPath;

	function __construct($rootPath)
	{
		$this->rootPath = $rootPath.'/';
		$this->galleryPath = $this->rootPath.'gallery/';
		$this->pyfacesPath = $this->rootPath.'pyfacescmd';
	}

	static public function freeFileName($dest, $origin, $forceNumber=false)
	{
		if ($forceNumber)
		{
			$fileext = (strpos($origin,'.')===false?'':'.'.substr(strrchr($origin, "."), 1));
			$filename = substr($origin, 0, strlen($origin)-strlen($fileext)).'_1'.$fileext;
		}
		else
		$filename = $origin;
		$fulldest = $dest.$filename;
		for ($i = 1; file_exists($fulldest); $i++)
		{
			$fileext = (strpos($origin,'.')===false?'':'.'.substr(strrchr($origin, "."), 1));
			$filename = substr($origin, 0, strlen($origin)-strlen($fileext)).'_'.$i.$fileext;
			$fulldest = $dest.$filename;
		}
		return $fulldest;
	}

	static public function uploadFile($temp, $dest, $filename, $forceNumber=false)
	{
		do {
			$fullname = self::freeFileName($dest, $filename, $forceNumber);
		} while (move_uploaded_file($temp, $fullname) == false);
		return $fullname;
	}

	public function uploadGallery($temp, $filename)
	{
		self::uploadFile($temp, $this->galleryPath, $filename, true);
	}

	public function uploadPreview($temp, $filename)
	{
		return self::uploadFile($temp, $this->rootPath, $filename);
	}
}